//
//  BN_UserHelper.m
//  BabyNow
//
//  Created by iMobile on 14-4-2.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_UserHelper.h"
#import "IMB_NetWorkJsonRespOperation.h"
#import "BN_UserInfo.h"
#import "IMB_FileHelper.h"

@interface BN_UserHelper()

/**
 *  登录请求队列
 */
@property (nonatomic,strong) NSOperationQueue *loginQueue;

/**
 *  获取最新的通知消息队列
 */
@property (nonatomic,strong) NSOperationQueue *queryNewNotificationsQueue;


@end

@implementation BN_UserHelper


#pragma mark - Property method

// 获取登录请求队列
- (NSOperationQueue*)loginQueue{
    if (!_loginQueue) {
        _loginQueue = [[NSOperationQueue alloc]init];
    }
    return _loginQueue;
}

// 获取最新通知消息队列
- (NSOperationQueue*)queryNewNotificationsQueue{
    if (!_queryNewNotificationsQueue) {
        _queryNewNotificationsQueue = [[NSOperationQueue alloc]init];
    }
    return _queryNewNotificationsQueue;
}

#pragma mark - Interface method

// 归档帐户信息
+ (void)archiverAccount:(BN_UserInfo *)account{
    [IMB_FileHelper archiver:account forKey:kUserInfoArchiver toFile:FILE_PATH_FOR_USER_ARCHIVER];
}

// 解档帐户信息
+ (BN_UserInfo*)unArchiverAccountInfo{
    return [IMB_FileHelper unArchiverFromFile:FILE_PATH_FOR_USER_ARCHIVER withKey:kUserInfoArchiver];
}

// 获取最新的通知消息
- (void)queryNewNotificationsWithCompleteHandler:(DataHandleComplete)handler{
    
    // 从本地获取帐户和密码信息
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_NEW_NOTIFICATION requestMethod:NET_REQUEST_GET paramsValues:@{kAccount: userInfo.account,kPassword:userInfo.password} completeHandler:^(id result, NSString *errorMessage) {       
        if (errorMessage) {
            handler(nil,errorMessage);
        }else{
            // 检查result中的errorCode值
            if ([result[kErrorCode] intValue] == 0) {
                handler(result[kInfo][0],nil);
            }else{
                handler(nil,result[kErrorMessage]);
            }
        }
        
    }];
    [self.queryNewNotificationsQueue addOperation:operation];
}

// 登录操作
- (void)loginWithAccount:(NSString *)account password:(NSString *)password completeHandler:(DataHandleComplete)handler{
    
    NSLog(@"%@",NET_IF_LOGIN);
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_LOGIN requestMethod:NET_REQUEST_POST paramsValues:@{kAccount: account,kPassword:password} completeHandler:^(id result, NSString *errorMessage) {
        if (errorMessage) {
            handler(nil,errorMessage);
        }else{
            
            BN_UserInfo *userInfo = [[BN_UserInfo alloc]initWithJsonDict:result];
            if (userInfo.errorCode && [userInfo.errorCode intValue] == 0) {
                // 将帐户和密码持久化到本地
                [BN_UserHelper archiverAccount:userInfo];
                
                // 检查result中的errorCode值
                handler(userInfo,nil);
                
            }else{
                handler(nil,userInfo.errorMessage);
            }
        }
    }];
    
    [self.loginQueue addOperation:operation];
}

@end
